//
//  NewAlbumViewController.m
//  GSG
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "NewAlbumViewController.h"
#import "UIImageView+WebCache.h"
#import "RecommendModel.h"
#import "NewAlbumCollectionViewCell.h"
#import "AFNetworkingTool.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import "songleiViewController.h"

@interface NewAlbumViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

{
    NSInteger number;
}

@property(nonatomic,retain)UICollectionView *myCollectionView;

@end

@implementation NewAlbumViewController

- (void)dealloc
{
    [_array release];
    [super dealloc];
}


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"推荐";
        number = 1;
        self.array = [NSMutableArray array];
    }
    
    return self;
}

- (void)data{
    NSString *str1 = @"http://online.dongting.com/recomm/new_songs_more?page=";
    NSString *str2 = [NSString stringWithFormat:@"%ld&size=30", number];
    NSString *str = [NSString stringWithFormat:@"%@%@", str1, str2];
    [AFNetworkingTool NetWorkingTooStr:str block:^(id result) {
        
        NSMutableArray *arr = [result objectForKey:@"data"];
        for (NSInteger i=0; i<arr.count; i++) {
            RecommendModel *model = [[RecommendModel alloc] init];
            NSMutableDictionary *movieDic = arr[i];
            [model setValuesForKeysWithDictionary:movieDic];
            [self.array addObject:model];
            [model release];
            
        }
        [self.myCollectionView reloadData];
        
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self data];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width * 100 / 416, self.view.frame.size.height * 100 / 416);
    
    //设置最小的行间距
    flowLayout.minimumLineSpacing = 70;
    //设置最小的列间距
    flowLayout.minimumInteritemSpacing = 10;
    //设置滚动的方向(默认是垂直)
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
     //设置一个item的边框,上下左右的边距大小
    flowLayout.sectionInset = UIEdgeInsetsMake(self.view.frame.size.width * 20 / 375, self.view.frame.size.height * 20 / 667, self.view.frame.size.width * 20 / 375, self.view.frame.size.height * 20 / 667);
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    
    self.myCollectionView.backgroundColor = [UIColor clearColor];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
    [self.view addSubview:self.myCollectionView];
    
#pragma mark collectionView只能采用注册的方式生成cell
    [self.myCollectionView registerClass:[NewAlbumCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    self.myCollectionView = self.myCollectionView;
    [self addFooter];
//    [self addHeader];
    
}

#pragma mark 协议里面有两个必须实现的协议的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewAlbumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    RecommendModel *mo=self.array[indexPath.row];
    cell.myLabel.text = mo.title;
    RecommendModel *model = self.array[indexPath.row];
    
    NSString *pic=model.pic;
    
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:pic]];
    
    
    return cell;
    
}

-(void)addHeader {
    [self.myCollectionView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.myCollectionView reloadData];
            
            [self.myCollectionView headerEndRefreshing];
        });
    }];
    [self.myCollectionView headerBeginRefreshing];
}

-(void)addFooter {
    [self.myCollectionView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            number++;
            [self data];
            [self.myCollectionView reloadData];
            
            [self.myCollectionView footerEndRefreshing];
        });
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    songleiViewController *song = [[songleiViewController alloc] init];
//    song.musicModel = self.array[indexPath.row] ;
    [self.navigationController pushViewController:song animated:YES];
    [song release];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
